<%#
 Copyright 2013-2018 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
apiVersion: apps/v1
kind: Deployment
metadata:
  name: <%= app.baseName.toLowerCase() %>
  namespace: <%= kubernetesNamespace %>
spec:
  replicas: 1
  selector:
    matchLabels:
      app: <%= app.baseName.toLowerCase() %>
      version: "v1"
  template:
    metadata:
      labels:
        app: <%= app.baseName.toLowerCase() %>
        version: "v1"
    spec:
      initContainers:
        - name: init-ds
          image: busybox
          command:
            - '/bin/sh'
            - '-c'
            - |
                while true
                do
                <%_ if (app.prodDatabaseType === 'mysql') { _%>
                  rt=$(nc -z -w 1 <%= app.baseName.toLowerCase() %>-mysql 3306)
                <%_ } _%>
                <%_ if (app.prodDatabaseType === 'mariadb') { _%>
                  rt=$(nc -z -w 1 <%= app.baseName.toLowerCase() %>-mariadb 3306)
                <%_ } _%>
                <%_ if (app.prodDatabaseType === 'postgresql') { _%>
                  rt=$(nc -z -w 1 <%= app.baseName.toLowerCase() %>-postgresql 5432)
                <%_ } _%>
                <%_ if (app.prodDatabaseType === 'mongodb') { _%>
                  rt=$(nc -z -w 1 <%= app.baseName.toLowerCase() %>-mongodb 27017)
                <%_ } _%>
                <%_ if (app.prodDatabaseType === 'couchbase') { _%>
                  rt=$(nc -z -w 1 <%= app.baseName.toLowerCase() %>-couchbase 8091)
                <%_ } _%>
                  if [ $? -eq 0 ]; then
                    echo "DB is UP"
                    break
                  fi
                  echo "DB is not yet reachable;sleep for 10s before retry"
                  sleep 10
                done
      containers:
      - name: <%= app.baseName.toLowerCase() %>-app
        image: <%= app.targetImageName %>
        env:
        - name: SPRING_PROFILES_ACTIVE
          value: prod
        <%_ if (app.serviceDiscoveryType === 'eureka') { _%>
        - name: SPRING_CLOUD_CONFIG_URI
          value: http://admin:${jhipster.registry.password}@jhipster-registry.<%= kubernetesNamespace %>.svc.cluster.local:8761/config
        - name: JHIPSTER_REGISTRY_PASSWORD
          valueFrom:
            secretKeyRef:
              name: registry-secret
              key: registry-admin-password
        - name: EUREKA_CLIENT_SERVICE_URL_DEFAULTZONE
          value: http://admin:${jhipster.registry.password}@jhipster-registry.<%= kubernetesNamespace %>.svc.cluster.local:8761/eureka/
          <%_ if (istio !== 'no') { _%>
        - name: EUREKA_INSTANCE_PREFER_IP_ADDRESS
          value: "false"
        - name: EUREKA_INSTANCE_HOSTNAME
          value: <%= app.baseName.toLowerCase() %>
          <%_ } _%>
        <%_ } _%>
        <%_ if (app.serviceDiscoveryType === 'consul') { _%>
        - name: SPRING_CLOUD_CONSUL_HOST
          value: consul.<%= kubernetesNamespace %>.svc.cluster.local
        - name: SPRING_CLOUD_CONSUL_PORT
          value: "8500"
          <%_ if (istio !== 'no') { _%>
        - name: SPRING_CLOUD_CONSUL_DISCOVERY_PREFER_IP_ADDRESS
          value: "false"
        - name: SPRING_CLOUD_CONSUL_DISCOVERY_HOSTNAME
          value: <%= app.baseName.toLowerCase() %>
        - name: SPRING_CLOUD_CONSUL_DISCOVERY_SERVICE_NAME
          value: <%= app.baseName.toLowerCase() %>
          <%_ } _%>
        <%_ } _%>
        <%_ if (!app.serviceDiscoveryType && app.authenticationType === 'jwt') { _%>
        - name: JHIPSTER_SECURITY_AUTHENTICATION_JWT_SECRET
          valueFrom:
            secretKeyRef:
              name: jwt-secret
              key: secret
        <%_ } _%>
        <%_ if (app.prodDatabaseType === 'mysql') { _%>
        - name: SPRING_DATASOURCE_URL
          value: jdbc:mysql://<%= app.baseName.toLowerCase() %>-mysql.<%= kubernetesNamespace %>.svc.cluster.local:3306/<%= app.baseName.toLowerCase() %>?useUnicode=true&characterEncoding=utf8&useSSL=false
        <%_ } _%>
        <%_ if (app.prodDatabaseType === 'mariadb') { _%>
        - name: SPRING_DATASOURCE_URL
          value: jdbc:mariadb://<%= app.baseName.toLowerCase() %>-mariadb.<%= kubernetesNamespace %>.svc.cluster.local:3306/<%= app.baseName.toLowerCase() %>
        <%_ } _%>
        <%_ if (app.prodDatabaseType === 'postgresql') { _%>
        - name: SPRING_DATASOURCE_URL
          value: jdbc:postgresql://<%= app.baseName.toLowerCase() %>-postgresql.<%= kubernetesNamespace %>.svc.cluster.local:5432/<%= app.baseName %>
        <%_ } _%>
        <%_ if (app.prodDatabaseType === 'mongodb') { _%>
        - name: SPRING_DATA_MONGODB_DATABASE
          value: <%= app.baseName %>
        - name: SPRING_DATA_MONGODB_URI
          value: "mongodb://<% for (let i = 0; i < app.dbPeerCount; i++) { %><%= app.baseName.toLowerCase() %>-mongodb-<%= i %>.<%= app.baseName.toLowerCase() %>-mongodb.<%= kubernetesNamespace %>:27017<% if (i < (app.dbPeerCount-1)) { %>,<% }} %>"
        <%_ } _%>
        <%_ if (app.prodDatabaseType === 'couchbase') { _%>
        - name: SPRING_COUCHBASE_BOOTSTRAP_HOSTS
          value: "<% for (let i = 0; i < app.dbPeerCount; i++) { %><%= app.baseName.toLowerCase() %>-couchbase-<%= i %>.<%= app.baseName.toLowerCase() %>-couchbase<% if (i < (app.dbPeerCount-1)) { %>,<% }} %>"
        - name: SPRING_COUCHBASE_BUCKET_NAME
          value: <%= app.baseName.toLowerCase() %>
        <%_ } _%>
        <%_ if (app.searchEngine === 'elasticsearch') { _%>
        - name: SPRING_DATA_JEST_URI
          value: http://<%= app.baseName.toLowerCase() %>-elasticsearch.<%= kubernetesNamespace %>.svc.cluster.local:9200
        <%_ } _%>
        <%_ if (app.messageBroker === 'kafka') { _%>
        - name: SPRING_CLOUD_STREAM_KAFKA_BINDER_BROKERS
          value: jhipster-kafka.<%= kubernetesNamespace %>.svc.cluster.local
        - name: SPRING_CLOUD_STREAM_KAFKA_BINDER_ZK_NODES
          value: jhipster-zookeeper.<%= kubernetesNamespace %>.svc.cluster.local
        <%_ } _%>
        <%_ if (monitoring === 'elk') { _%>
        - name: JHIPSTER_METRICS_LOGS_ENABLED
          value: 'true'
        - name: JHIPSTER_LOGGING_LOGSTASH_ENABLED
          value: 'true'
        - name: JHIPSTER_LOGGING_LOGSTASH_HOST
          value: jhipster-logstash
            <%_ if (composeApplicationType === 'microservice') { _%>
        - name: SPRING_ZIPKIN_ENABLED
          value: "true"
        - name: SPRING_ZIPKIN_BASE_URL
          value: http://jhipster-zipkin
            <%_ } _%>
        <%_ } _%>
        <%_ if (monitoring === 'prometheus') { _%>
        - name: JHIPSTER_METRICS_PROMETHEUS_ENABLED
          value: 'true'
        - name: JHIPSTER_METRICS_PROMETHEUS_ENDPOINT
          value: /prometheusMetrics
        <%_ } _%>
        <%_ if (composeApplicationType === 'microservice') { _%>
        - name: SPRING_SLEUTH_PROPAGATION_KEYS
          value: "x-request-id,x-ot-span-context"
        <%_ } _%>
        - name: JAVA_OPTS
          value: " -Xmx256m -Xms256m"
        resources:
          requests:
            memory: "256Mi"
            cpu: "500m"
          limits:
            memory: "512Mi"
            cpu: "1"
        ports:
        - name: http
          containerPort: <%= app.serverPort %>
        readinessProbe:
          httpGet:
            path: <%= app.applicationType === 'microservice' && !app.serviceDiscoveryType ? "/" + app.baseName.toLowerCase() : "" %>/management/health
            port: http
          initialDelaySeconds: 20
          periodSeconds: 15
          failureThreshold: 6
        livenessProbe:
          httpGet:
            path: <%= app.applicationType === 'microservice' && !app.serviceDiscoveryType ? "/" + app.baseName.toLowerCase() : "" %>/management/health
            port: http
          initialDelaySeconds: 120
